package org.mock.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.mock.base.LambdaRequestHandler;
import org.mock.base.RequestExecutor;
import org.mock.dto.*;
import org.mock.entity.*;
import org.mock.service.*;
import org.springframework.stereotype.Service;

/**
 * 用户表 服务实现类
 *
 * @author AI
 * @since 2020-06-19
 */
@Service
@AllArgsConstructor
@Slf4j
public class SabreServiceImpl implements SabreService {

    private SabreHotelDetailService sabreHotelDetailService;
    private SabreHotelAvailService sabreHotelAvailService;
    private SabreHotelPriceCheckService sabreHotelPriceCheckService;
    private SabreCreatePassengerService sabreCreatePassengerService;
    private SabreOrderBookingService sabreOrderBookingService;
    private SabreOrderCancelBookingService sabreOrderCancelBookingService;

    @Override
    public String getHotelAvail(JSONObject request) {
        return RequestExecutor.execute(
                request,
                new LambdaRequestHandler<>(
                        sabreHotelAvailService::handleRequest,
                        sabreHotelAvailService::handleQueryWrapper,
                        sabreHotelAvailService::getOne,
                        sabreHotelAvailService::handleData
                )
        );
    }

    @Override
    public String getHotelDetails(JSONObject request) {
        //解析出初始查询条件
        SabreHotelDetailDTO originalQuery = sabreHotelDetailService.handleRequest(request);
        //根据mock场景，加工查询条件
        SabreHotelDetailDTO queryWrapper = sabreHotelDetailService.handleQueryWrapper(originalQuery);
        //查询数据
        SabreHotelDetail data = sabreHotelDetailService.getOne(queryWrapper);
        //加工数据
        return sabreHotelDetailService.handleData(data, originalQuery);
    }

    @Override
    public String priceCheck(JSONObject request) {
        return RequestExecutor.execute(
                request,
                new LambdaRequestHandler<>(
                        sabreHotelPriceCheckService::handleRequest,
                        sabreHotelPriceCheckService::handleQueryWrapper,
                        sabreHotelPriceCheckService::getOne,
                        sabreHotelPriceCheckService::handleData
                )
        );
    }

    @Override
    public String createPnr(JSONObject request) {
        log.info("[createPnr] 请求：{}", request);
        //第几次调用，就返回第几次的结果
        SabreCreatePassenger one = sabreCreatePassengerService.getOne(new SabreCreatePassengerDTO());
        if (null == one) {
            return null;
        }
        Integer callCount = one.getCallCount();
        SabreCreatePassengerDTO updateDto = new SabreCreatePassengerDTO();
        updateDto.setId(one.getId());
        if (callCount == 1) {
            log.info("当前是第1次调用下单");
            //次数置为2
            updateDto.setCallCount(2);
            sabreCreatePassengerService.updateById(updateDto);
            return one.getResponseFirst();
        } else {
            log.info("当前是第2次调用下单");
            //次数重置为1
            updateDto.setCallCount(1);
            sabreCreatePassengerService.updateById(updateDto);
            return one.getResponseSecond();
        }
    }

    @Override
    public String getBooking(JSONObject request) {
        return RequestExecutor.execute(
                request,
                new LambdaRequestHandler<>(
                        //解析请求体
                        sabreOrderBookingService::handleRequest,
                        //根据配置，修改查询条件
                        sabreOrderBookingService::handleQueryWrapper,
                        //查询
                        sabreOrderBookingService::getOne,
                        //处理返回体
                        sabreOrderBookingService::handleData
                )
        );
    }

    @Override
    public String cancelBooking(JSONObject request) {
        //解析出初始查询条件
        SabreOrderCancelBookingDTO originalQuery = sabreOrderCancelBookingService.handleRequest(request);
        //根据mock场景，加工查询条件
        SabreOrderCancelBookingDTO queryWrapper = sabreOrderCancelBookingService.handleQueryWrapper(originalQuery);
        //查询数据
        SabreOrderCancelBooking data = sabreOrderCancelBookingService.getOne(queryWrapper);
        //加工数据
        return sabreOrderCancelBookingService.handleData(data, originalQuery);
    }
}
